#import "SMRVerticallyAlignedTextFieldCell.h"

static NSString* const SMRVerticallyAlignedTextFieldCellVerticalAlignmentKey = @"SMRVerticallyAlignedTextFieldCellVerticalAlignmentKey";


@implementation SMRVerticallyAlignedTextFieldCell

@synthesize verticalAlignment;


-(id)initWithCoder:(NSCoder *)aDecoder {
	if (!(self = [super initWithCoder:aDecoder]))
		return nil;
	[self setVerticalAlignment:(SMRVerticalTextAlignment)[aDecoder decodeIntegerForKey:SMRVerticallyAlignedTextFieldCellVerticalAlignmentKey]];
	return self;
}

-(void)encodeWithCoder:(NSCoder *)aCoder {
	[super encodeWithCoder:aCoder];
	[aCoder encodeInteger:[self verticalAlignment] forKey:SMRVerticallyAlignedTextFieldCellVerticalAlignmentKey];
}

-(NSRect)titleRectForBounds:(NSRect)bounds {
	NSRect titleRect = [super titleRectForBounds:bounds];
	CGFloat stringHeight = [[self attributedStringValue] size].height;
	if ([self verticalAlignment] == SMRCenterTextAlignment) 
		titleRect.origin.y = bounds.origin.y + ((bounds.size.height - stringHeight) / 2.0f);
	else if ([self verticalAlignment] == SMRBottomTextAlignment)
		titleRect.origin.y += titleRect.size.height - stringHeight;
	return titleRect;
}

-(void)drawInteriorWithFrame:(NSRect)cellFrame inView:(NSView *)controlView {
	NSRect titleRect = [self titleRectForBounds:cellFrame];
	[[self attributedStringValue] drawInRect:titleRect];
}

- (void)editWithFrame:(NSRect)aRect inView:(NSView *)controlView editor:(NSText *)textObj delegate:(id)anObject event:(NSEvent *)theEvent {
    NSRect titleRect = [self titleRectForBounds:aRect];
    titleRect.origin.x += 2;
    [super editWithFrame:titleRect inView:controlView editor:textObj delegate:anObject event:theEvent];
}

- (void)selectWithFrame:(NSRect)aRect inView:(NSView *)controlView editor:(NSText *)textObj delegate:(id)anObject start:(NSInteger)selStart length:(NSInteger)selLength {
    NSRect titleRect = [self titleRectForBounds:aRect];
    titleRect.origin.x += 2;
    [super selectWithFrame:titleRect inView:controlView editor:textObj delegate:anObject start:selStart length:selLength];
}

@end
